﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BearWare;
using System.ComponentModel.Composition;
using RemoteTranslation.AssistentApplication.Configuration;

namespace RemoteTranslation.AssistentApplication.Services.TT4
{
    [Export(typeof(ITranslatorAssistent))]
    public class TeamTalk4TranslatorAssistent : ITranslatorAssistent
    {
        #region Fields

        private ServerInformation _serverInformation;
        private TeamTalk4 _teamTalk4Client = null;

        #endregion

        #region Constructors

        public TeamTalk4TranslatorAssistent()
        {
            #region Init Server Information

            ConnectedServer = new ServerInformation();
            ConnectedServer.IsConnected = false;
            ConnectedServer.HostAddress = AssistentApplicationConfigurationSection.Instance.Server.ServerHostAddress;
            ConnectedServer.TcpPort = AssistentApplicationConfigurationSection.Instance.Server.ServerTcpPort;
            ConnectedServer.UdpPort = AssistentApplicationConfigurationSection.Instance.Server.ServerUdpPort;

            #endregion

            #region Input Audio Channels

            List<ChannelInformation> inputAudioChannels = new List<ChannelInformation>();
            inputAudioChannels.Add(
                new ChannelInformation()
                {
                    Name = "Hebrew",
                    Codec = new MusicAudioCodecInformation()
                    {
                        SampleRate = 44100,
                        ChannelMode = ChannelMode.Stereo
                    },
                    AudioConfiguration = new AudioConfiguration()
                    {
                        EnableAGC = true,
                        GainLevel = 32000,
                        MaxIncDBSec = 12,
                        MaxDecDBSec = -40,
                        MaxGainDB = 30
                    }
                }
            );
            inputAudioChannels.Add(
                new ChannelInformation()
                {
                    Name = "Russian"
                }
            );
            inputAudioChannels.Add(
                new ChannelInformation()
                {
                    Name = "English"
                }
            );
            _serverInformation.InputAudioChannels = inputAudioChannels;

            #endregion

            #region Input Video Channel

            ChannelInformation videoChannel = new ChannelInformation()
            {
                Name = "Video",
                Codec = new NoCodecInformation(),
                AudioConfiguration = new AudioConfiguration()
                {
                    EnableAGC = true,
                    GainLevel = 32000,
                    MaxIncDBSec = 12,
                    MaxDecDBSec = -40,
                    MaxGainDB = 30
                }
            };
            _serverInformation.InputVideoChannel = videoChannel;

            #endregion

            #region Output Audio Channels



            #endregion

            #region License Information

            // Set license information before creating the client instance
            TeamTalk4.SetLicenseInformation(
                AssistentApplicationConfigurationSection.Instance.Server.LicenseInformation.RegistrationName,
                AssistentApplicationConfigurationSection.Instance.Server.LicenseInformation.RegistrationKey
            );

            #endregion

            // we pass 'false' to poll_events since we don't want to 
            // manually process events using _teamTalk4Client.GetMessage
            _teamTalk4Client = new TeamTalk4(false);
        }

        #endregion

        #region ITranslatorAssistent Properties

        public ServerInformation ConnectedServer
        {
            get { return _serverInformation; }

            protected set
            {
                _serverInformation = value;
            }
        }

        #endregion

        #region IDisposable Implementation

        public void Dispose()
        {
            if (_teamTalk4Client != null)
            {
                _teamTalk4Client.Dispose();
                _teamTalk4Client = null;
            }
        }

        #endregion

        #region ITranslatorAssistent Methods

        public void Connect()
        {
            ConnectedServer.IsConnected = _teamTalk4Client.Connect(
                ConnectedServer.HostAddress,
                ConnectedServer.TcpPort,
                ConnectedServer.UdpPort,
                0, // local tcp port
                0  // local udp port
            );

            if (ConnectedServer.IsConnected)
            {
            }
            else
            {
            }
        }

        public void Disconnect()
        {
            ConnectedServer.IsConnected = !_teamTalk4Client.Disconnect();
        }

        #endregion

        #region Private Methods

        void _teamTalk4Client_OnUserVideoFrame(int nUserID, int nFrameQueueSize)
        {
        }

        #endregion
    }
}
